﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Utils;
using COFE.Helpers.Subclasses;
using COFE.Core;

namespace COFE.Helpers
{
    public static class FS
    {
        #region Methods

        private static T checkType<T>(string input, object obj, bool throwException)
        {
            if (!(obj is T))
                if (throwException)
                    throw new ArgumentException(String.Format("The type of {0} is {1}", input, obj.GetType()));
                else return default(T);

            return (T)obj;
        }
        
        public static IFileSystemInfo ParsePath(string path)
        {
            return COFEShared.CurrentUserProfile.ParsePath(path);
        }


        public static IEnumerable<IFileSystemInfo> Search(string filterString)
        {
            return new AutoEntryList("search", filterString).Entries;
        }

        public static IFSI ParsePath<IFSI>(string path) where IFSI : IFileSystemInfo
        {
            var retVal = COFEShared.CurrentUserProfile.ParsePath(path);
            return checkType<IFSI>(path, retVal, true);
        }

        public static IFileSystemInfo TryParsePath(string path)
        {
            return COFEShared.CurrentUserProfile.TryParsePath(path);
        }

        public static IFSI TryParsePath<IFSI>(string path) where IFSI : IFileSystemInfo
        {
            var retVal = COFEShared.CurrentUserProfile.TryParsePath(path);
            if (retVal == null)
                return default(IFSI);
            return checkType<IFSI>(path, retVal, false);
        }

        #endregion

        #region Data

        private static Path _path = new Path();

        #endregion

        #region Public Property

        public static IFileServices File { get { return COFEShared.CurrentUserProfile.File; } }
        public static IDirectoryServices Directory { get { return COFEShared.CurrentUserProfile.Directory; } }
        public static Path Path { get { return _path; } }
        public static IEntryListManager EntryLists { get { return COFEShared.CurrentUserProfile.EntryListManager; } }
        #endregion
    }
}
